<?php
/**
 * The custom logo script.
 *
 * @package WordPress
 * @subpackage Administration
 */

/**
 * The custom logo class.
 *
 * @since 3.0.0
 * @package WordPress
 * @subpackage Administration
 */
class Custom_logo {

	/**
	 * Callback for administration header.
	 *
	 * @var callback
	 * @since unknown
	 * @access private
	 */
	var $admin_header_callback;

	/**
	 * Callback for header div.
	 *
	 * @var callback
	 * @since 3.0.0
	 * @access private
	 */
	var $admin_image_div_callback;

	/**
	 * Holds the page menu hook.
	 *
	 * @var string
	 * @since 3.0.0
	 * @access private
	 */
	var $page = '';

	/**
	 * PHP4 Constructor - Register administration header callback.
	 *
	 * @since 3.0.0
	 * @param callback $admin_header_callback
	 * @param callback $admin_image_div_callback Optional custom image div output callback.
	 * @return Custom_Logo
	 */
	function Custom_logo($admin_header_callback = '', $admin_image_div_callback = '') {
		$this->admin_header_callback = $admin_header_callback;
		$this->admin_image_div_callback = $admin_image_div_callback;
	}

	/**
	 * Set up the hooks for the Custom Logo admin page.
	 *
	 * @since 3.0.0
	 */
	function init() {
		if ( ! current_user_can('edit_theme_options') )
			return;

		$this->page = $page = add_theme_page(__('Logo'), __('Logo'), 'edit_theme_options', 'custom-logo', array(&$this, 'admin_page'));

		add_action("load-$page", array(&$this, 'admin_load'));
		add_action("load-$page", array(&$this, 'take_action'), 49);
		add_action("load-$page", array(&$this, 'handle_upload'), 49);

		if ( $this->admin_header_callback )
			add_action("admin_head-$page", $this->admin_header_callback, 51);
	}

	/**
	 * Set up the enqueue for the CSS & JavaScript files.
	 *
	 * @since 3.0.0
	 */
	function admin_load() {
		add_contextual_help( $this->page, '<p>' . __( 'You can customize the logo of your site without touching any of your theme&#8217;s code by using a custom logo.' ) . '</p>' .
		'<p>' . __( 'To use a custom logo, simply upload it.' ) . '</p>');
		wp_enqueue_script('custom-logo');
		wp_enqueue_style('farbtastic');
	}

	/**
	 * Execute custom background modification.
	 *
	 * @since 3.0.0
	 */
	function take_action() {

		if ( empty($_POST) )
			return;

		if ( isset($_POST['reset-logo']) ) {
			check_admin_referer('custom-logo-reset', '_wpnonce-custom-logo-reset');
			remove_theme_mod_tm('logo_image');
			$this->updated = true;
			return;
		}

		if ( isset($_POST['remove-logo']) ) {
			// @TODO: Uploaded files are not removed here.
			check_admin_referer('custom-logo-remove', '_wpnonce-custom-logo-remove');
			set_theme_mod_tm('logo_image', '');
			$this->updated = true;
			return;
		}

		$this->updated = true;
	}

	/**
	 * Display the custom background page.
	 *
	 * @since 3.0.0
	 */
	function admin_page() {
?>
<div class="wrap" id="custom-logo">
<?php screen_icon(); ?>
<h2><?php _e('Custom Logo'); ?></h2>
<?php if ( !empty($this->updated) ) { ?>
<div id="message" class="updated">
<p><?php printf( __( 'Logo updated. <a href="%s">Visit your site</a> to see how it looks.' ), home_url( '/' ) ); ?></p>
</div>
<?php }


?>
<table class="form-table">
<tbody>
<?php if ( get_theme_mod_tm('logo_image', '') ) : ?>
<tr valign="top">
<th scope="row"><?php _e('Preview'); ?></th>
<td>
<?php
	if ( $this->admin_image_div_callback ) {
		call_user_func($this->admin_image_div_callback);
	} else {
	$logo_styles = '';

	if ( get_theme_mod_tm('logo_image', '') ) {
		// LOGO URL must be single quote, see below
		$logo_styles .= ' background-image: url(\'' . get_theme_mod_tm('logo_image', '') . '\');'
			. ' background-repeat: no-repeat;'
			. ' background-position: top left; height:150px; ';
	}
	?>
	<?php if ( get_theme_mod_tm('logo_image', '') ) { ?>
    <div style="<?php echo $logo_styles; ?>"><?php // must be double quote, see above ?></div>
    <?php } ?>
<?php } ?>
</td>
</tr>
<?php if ( get_theme_mod_tm('logo_image', '') ) : ?>
<tr valign="top">
<th scope="row"><?php _e('Remove Image'); ?></th>
<td>
<form method="post" action="">
<?php wp_nonce_field('custom-logo-remove', '_wpnonce-custom-logo-remove'); ?>
<input type="submit" class="button" name="remove-logo" value="<?php esc_attr_e('Remove Logo Image'); ?>" /><br/>
<?php _e('This will remove the logo image.') ?>
</form>
</td>
</tr>
<?php endif; endif; ?>

<tr valign="top">
<th scope="row"><?php _e('Upload Image'); ?></th>
<td><form enctype="multipart/form-data" id="upload-form" method="post" action="">
<label for="upload"><?php _e('Choose an image from your computer:'); ?></label><br /><input type="file" id="upload" name="import" />
<input type="hidden" name="action" value="save" />
<?php wp_nonce_field('custom-logo-upload', '_wpnonce-custom-logo-upload') ?>
<input type="submit" class="button" value="<?php esc_attr_e('Upload'); ?>" />
</p>
</form>
</td>
</tr>
</tbody>
</table>

</div>
<?php
	}

	/**
	 * Handle a Image upload for the background image.
	 *
	 * @since 3.0.0
	 */
	function handle_upload() {

		if ( empty($_FILES) )
			return;

		check_admin_referer('custom-logo-upload', '_wpnonce-custom-logo-upload');
		$overrides = array('test_form' => false);
		$file = wp_handle_upload($_FILES['import'], $overrides);

		if ( isset($file['error']) )
			wp_die( $file['error'] );

		$url = $file['url'];
		$type = $file['type'];
		$file = $file['file'];
		$filename = basename($file);

		// Construct the object array
		$object = array(
			'post_title' => $filename,
			'post_content' => $url,
			'post_mime_type' => $type,
			'guid' => $url
		);

		// Save the data
		$id = wp_insert_attachment($object, $file);

		// Add the meta-data
		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

		set_theme_mod_tm('logo_image', esc_url($url));

		do_action('wp_create_file_in_uploads', $file, $id); // For replication
		$this->updated = true;
	}

}
?>
